package com.quanyan.riding.service.impl;

import com.github.ltsopensource.core.json.JSON;
import com.quanyan.api.APIResponse;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.common.utils.DateUtils;
import com.quanyan.riding.entity.db.TbRidingRecord;
import com.quanyan.riding.entity.db.TbRidingRecordExample;
import com.quanyan.riding.entity.vo.req.ReqRidingRecord;
import com.quanyan.riding.entity.vo.req.ReqRidingTrack;
import com.quanyan.riding.entity.vo.resp.RespRidingRecord;
import com.quanyan.riding.entity.vo.resp.RespRidingRecordMonth;
import com.quanyan.riding.mapper.MyTbRidingRecordMapper;
import com.quanyan.riding.mapper.TbRidingRecordMapper;
import com.quanyan.riding.mapper.TbUserMapper;
import com.quanyan.riding.service.RidingSingleService;
import com.quanyan.riding.util.RunDateUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xian-guo
 * @email 498152332@qq.com
 * @date 2016/8/29.
 * 骑行记录数据处理
 */
@Service("ridingSingleServiceImpl")
public class RidingSingleServiceImpl implements RidingSingleService {

    private static final Logger LOGGER = LoggerFactory.getLogger(RidingSingleServiceImpl.class);

    /**
     * 用户骑行详情记录
     */
    @Autowired
    private TbRidingRecordMapper tbRidingRecordMapper;

    /**
     * 用户骑行详情记录，手写sql
     */
    @Autowired
    private MyTbRidingRecordMapper myTbRidingRecordMapper;

    /**
     * 用户表
     */
    @Autowired
    private TbUserMapper tbUserMapper;

    /**
     * 骑行结束插入骑行详细记录，还有路书骑行结束也要插入骑行生涯记录
     * @param reqRidingTrack
     * @return
     */
    @Override
    @Transactional
    public APIResponse insertRidingTrack(ReqRidingTrack reqRidingTrack) {
        TbRidingRecord tbRidingRecord=new TbRidingRecord();
        try {
            //骑行时间转换
            tbRidingRecord.setStartTime(DateUtils.DAY_DATE_FORMAT_HH_MM_SS.parse(reqRidingTrack.getBeginDate()));
            tbRidingRecord.setEndTime(DateUtils.DAY_DATE_FORMAT_HH_MM_SS.parse(reqRidingTrack.getEndDate()));
            BeanUtils.copyProperties(tbRidingRecord,reqRidingTrack);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int insertRecod=tbRidingRecordMapper.insertSelective(tbRidingRecord);
        LOGGER.info("------插入骑行记录------->>>>>>>>>>>"+insertRecod);
        return APIResponse.returnSuccess();
    }

    /**
     * 骑行生涯记录
     * @param reqRidingRecord
     * @return
     */
    @Override
    public APIResponse<PageObj<List<RespRidingRecordMonth>>> getMonthRidingRecord(ReqRidingRecord reqRidingRecord) {
        Map<String,Object> paramMap=new HashMap<>();
        paramMap.put("userId",reqRidingRecord.getUserId());
        paramMap.put("pageNum",reqRidingRecord.getPageNum() * reqRidingRecord.getPageSize());
        paramMap.put("pageSize",reqRidingRecord.getPageSize());
        //查询用户骑行的详细记录
        List<RespRidingRecord> respRidingRecordList=myTbRidingRecordMapper.getRidingRecord(paramMap);
        //查询月骑行的总记录
        paramMap.put("startTime", respRidingRecordList.get(respRidingRecordList.size()-1).getStartTime());
        List<RespRidingRecordMonth> respRidingRecordMonths= myTbRidingRecordMapper.getRidingRecordMoth(paramMap);
        //找出某个月份对应的记录数
        for (RespRidingRecordMonth respRidingRecordMon:respRidingRecordMonths){
            String dateMonth=respRidingRecordMon.getDateMonth();
            List<RespRidingRecord> respRidingRecords=respRidingRecordMon.getRespRidingRecords();
            for (RespRidingRecord respRidingReco:respRidingRecordList){
                    String startTime= RunDateUtils.transferDateToString(respRidingReco.getStartTime());
                    if(startTime.equals(dateMonth)){
                        respRidingRecords.add(respRidingReco);
                    }
            }

        }
        TbRidingRecordExample tbRidingRecordExample=new TbRidingRecordExample();
        tbRidingRecordExample.createCriteria().andUserIdEqualTo(reqRidingRecord.getUserId());
        int totalCount=tbRidingRecordMapper.countByExample(tbRidingRecordExample);
        PageObj<List<RespRidingRecordMonth>> result= PageObj.create(totalCount,reqRidingRecord.getPageNum(),reqRidingRecord.getPageSize(),respRidingRecordMonths);
        LOGGER.debug(totalCount+"------------>>>>>>>>>>>>>"+ JSON.toJSONString(respRidingRecordMonths));
        return APIResponse.returnSuccess(result);
    }
}
